package com.edu.cloud.center.web;

import com.edu.cloud.center.api.LoginVO;
import com.edu.cloud.center.api.R;
import com.edu.cloud.center.utils.RedisUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class LoginController {
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginVO lvo) {
        if(!"abc".equals(lvo.getUsername())){
            return R.ok("username or password error");
        }
        // 单点登录情况下这里需要并发控制
        String loginKey="login:"+lvo.getUsername();
        String oldsid=RedisUtils.get(loginKey);
        if(StringUtils.isNotBlank(oldsid)){
            RedisUtils.del(oldsid);
            RedisUtils.del(loginKey);
        }
        String sessionId= UUID.randomUUID().toString();
        RedisUtils.set(sessionId,"{uid:123,username:abc}");
        RedisUtils.set(loginKey,sessionId);
        
        return R.ok(sessionId);
    }
}
